package org.apache.ocean.main.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ocean.main.http.HttpParameters;

import com.Ostermiller.util.CGIParser;

public class Util {
	public final static Pattern splitPattern = Pattern.compile(",| ");
  
	public static List<String> readLines(Reader reader) throws Exception {
		return IOUtils.readLines(reader);
	}
	
	public static void throwAsIOException(Throwable throwable) throws IOException {
		IOException ioException = new IOException(throwable.getMessage());
		ioException.initCause(throwable);
		throw ioException;
	}
	
	public static void copy(InputStream is, OutputStream os, byte[] buf) throws IOException {
		int numRead;
		while ((numRead = is.read(buf)) >= 0) {
			os.write(buf, 0, numRead);
		}
	}
  
	public static int[] toIntArray(List<Integer> list) {
		int size = list.size();
		int[] array = new int[size];
		int x = 0;
		for (int i : list) {
			array[x] = i;
			x++;
		}
		return array;
	}
	
	public static List<URL> loadUrls(File file) throws IOException {
		List<String> lines = IOUtils.readLines(new FileReader(file));
		List<URL> urls = new ArrayList<URL>();
		for (String line : lines) {
			urls.add(new URL(line));
		}
		return urls;
	}

	public static HttpParameters toHttpParameters(HttpServletRequest request) {
		try {
			if (StringUtils.equalsIgnoreCase("post", request.getMethod())) {
				HttpParameters parameters = new HttpParameters();
				URL url = new URL(request.getRequestURL().toString());
				CGIParser cgiParser = new CGIParser(url.toString(), "UTF-8");
				for (String name : cgiParser.getParameterNameList()) {
					for (String value : cgiParser.getParameterValues(name)) {
						parameters.add(name, value);
					}
				}
				return parameters;
			}
		} catch (Exception exception) {
			throw new RuntimeException(exception);
		}
		if (StringUtils.equalsIgnoreCase("get", request.getMethod())) {
			HttpParameters parameters = new HttpParameters();
			Enumeration paramEnum = request.getParameterNames();
			while (paramEnum.hasMoreElements()) {
				String name = (String) paramEnum.nextElement();
				String[] array = request.getParameterValues(name);
				if (array != null && array.length > 0) {
					for (String value : array) {
						parameters.add(name, value);
					}
				}
			}
			return parameters;
		}
		throw new RuntimeException("unknown http method " + request.getMethod());
	}

	public static Long getNextServerSequence(Long value, int serverNumber) {
		if (value == null) {
			return new Long(serverNumber);
		}
		Long i = null;
		if (value > 99) {
			String string = value.toString();
			String substring = string.substring(0, string.length() - 2);
			i = new Long(substring + "00");
		} else {
			i = new Long(0);
		}
		long v = i + serverNumber;
		return v + 100;
	}

	public static int getServerNumber(BigInteger id) {
		String string = id.toString();
		String substring = string.substring(string.length() - 2, string.length());
		return Integer.parseInt(substring);
	}

	public static SortedSet<String> splitToSortedSet(String string) {
		if (StringUtils.isBlank(string)) {
			return null;
		}
		String[] array = splitPattern.split(string.trim(), 0);
		TreeSet<String> sortedSet = new TreeSet<String>();
		for (int x = 0; x < array.length; x++) {
			sortedSet.add(array[x]);
		}
		return sortedSet;
	}

	public static File getAppServerHome() {
		return new File(System.getProperty("catalina.home"));
	}

	public static File getHomeDirectory(String name, File defaultDirectory) throws Exception {
		String value = System.getenv(name);
		if (value != null)
			return new File(value);
		value = System.getProperty(name);
		if (value != null)
			return new File(value);
		Context context = (Context) new InitialContext().lookup("java:comp/env");
		try {
			String string = (String) context.lookup(name);
			if (StringUtils.isNotBlank(value))
				return new File(value);
		} catch (NameNotFoundException nameNotFoundException) {
		}
		defaultDirectory.mkdirs();
		return defaultDirectory;
	}

	public static Object getFirst(List list) {
		Iterator iterator = list.iterator();
		if (iterator.hasNext()) {
			return iterator.next();
		} else {
			return null;
		}
	}

	public static Map<String, String> toMapExcept(org.jdom.Element element, String exceptName) {
		Map<String, String> map = new HashMap<String, String>();
		for (Object object : element.getAttributes()) {
			org.jdom.Attribute attribute = (org.jdom.Attribute) object;
			String name = attribute.getName();
			if (!StringUtils.equals(name, exceptName)) {
				map.put(name, attribute.getValue());
			}
		}
		return map;
	}
}
